function gotoadd(dom){
	var dialog = editAdminDataDialog2(dom,label_add,"/adminconsole/UserManager/User/add.jsp","addDialog",AddEventClick,loadElementFinish,null);
}

function loadElementFinish(dialog){
	setPositionDialog(dialog);
	fillDialogFrom(dialog);
	LoadDepartmentAndUserLevel(dialog);
}

function AddEventClick(dialog,id){
	var pass=  dialog.find("#pass").val();
	var confirmpass = dialog.find("#confirm").val();

	if(pass != confirmpass){
		simpleDialog("Password does not same");
		return false;
	}
	
	var name = dialog.find("#name").val();
	if(name == ""){
		return false;
	}
	var lastname = dialog.find("#lastname").val();
	if(lastname == ""){
		return false;
	}
	var phone = dialog.find("#phone").val();
	if(phone == ""){
		return false;
	}
	waitingDialog({});
	var address = dialog.find("#address").val();
	var country = dialog.find("#country").val();
	var state = dialog.find("#state").val();
	var city = dialog.find("#city").val();
	var zipcode = dialog.find("#zipcode").val();
	var mobile = dialog.find("#mobile").val();
	var emergency = dialog.find("#emergency").val();
	var gender = dialog.find("#gender").val();
	var dateb = dialog.find("#year").val() + "-"+(parseInt(dialog.find("#month").val())+1)+
	"-"+dialog.find("#date").val();
	var dateborn = dateb;
	var driver = dialog.find("#driver").val();
	var passport = dialog.find("#passport").val();
	var socialnumber = dialog.find("#socialnumber").val();
	var idnumber = dialog.find("#idnumber").val();
	var blood = dialog.find("#blood").val();
	var designation = dialog.find("#designation").val();
	var datejoin = dialog.find("[name=srTxtFrom]").attr("date");
	var email = dialog.find("#email").val();
	var userlevel = dialog.find("#userlevel").val();
	var department = dialog.find("#department").val();
	
	if($("[name=maxEntries]").val() == null){
		var line = 10;
	}else{
		var line = $("[name=maxEntries]").val();
	}
	if($("[name=selectpage]").val() == null){
		var pagePointer = 1;
	}else{
		var pagePointer = $("[name=selectpage]").val();
	}
	var url = "/userService";
	if(id == null){
		var func = "add";
	}else{
		var func = "edit"
	}
	var data = { func : func,
			id : id,
			address : address,
			country : country,
			state : state,
			city : city,
			zipCode : zipcode,
			mobile : mobile,
			emergencyNumber : emergency,
			gender : gender,
			birthday : dateborn,
			driverLicense : driver,
			passportNumber : passport,
			socialSecurityCard : socialnumber,
			identificationNo : idnumber,
			bloodGroup : blood,
			designation : designation,
			dateOfJoin : datejoin,
			firstName : name,
			lastName : lastname,
			phone : phone,
			department : department,
			userlevel : userlevel,
			email : email,
			pass : pass,
			offset: (line*pagePointer)-line,
			line: line,
			pagePointer: pagePointer
		}
	simpleservice(url,data,showUserList);
}

function EditDialog(dom,id){
	var dialog = editAdminDataDialog2(dom,label_add,"/adminconsole/UserManager/User/add.jsp","addDialog",AddEventClick,loadEditElementFinish,id);
}

function loadEditElementFinish(dialog,id){
	setPositionDialog(dialog);	
	fillDialogFrom(dialog);
	LoadDepartmentAndUserLevel(dialog);
	EditUser(id,dialog);
}

function EditUser(id,dialog){
	var url = "/userService";
	var data = { func : "getById",
			id : id
			}
	simpleserviceSyn(url,data,editSuccess,dialog);
}

function editSuccess(data,dialog){
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	dialog.find("#name").val(objArray2["firstName"]);
	dialog.find("#lastname").val(objArray2["lastName"]);
	dialog.find("#phone").val(objArray2["phone"]);
	dialog.find("#address").val(objArray2["address"]);
	dialog.find("#country").val(objArray2["countryCodeISO3"]);
	print_state_bydom(dialog.find("#state"),dialog.find("#country").val());
	dialog.find("#state").val(objArray2["state"]);
	dialog.find("#city").val(objArray2["city"]);
	dialog.find("#zipcode").val(objArray2["zipCode"]);
	dialog.find("#mobile").val(objArray2["mobile"]);
	dialog.find("#emergency").val(objArray2["emergencyNumber"]);
	dialog.find("#gender").val(objArray2["gender"]);
	var birthday = new Date(objArray2["birthday"]).toSystemFormat().split("-");
	dialog.find("#date").val(birthday[2]);
	dialog.find("#month").val(parseInt(birthday[1])-1);
	dialog.find("#year").val(birthday[0]);
	dialog.find("#driver").val(objArray2["driverlicense"]);
	dialog.find("#passport").val(objArray2["passportNumber"]);
	dialog.find("#socialnumber").val(objArray2["socialSecuritycard"]);
	dialog.find("#idnumber").val(objArray2["identificationNo"]);
	dialog.find("#blood").val(objArray2["bloodGroup"]);
	dialog.find("#designation").val(objArray2["designation"]);
	var dateofjoin = new Date(objArray2["dateOfJoin"]);
	dialog.find("[name=srTxtFrom]").attr("date",dateofjoin.toSystemFormat());
	dialog.find("[name=srTxtFrom]").val(dateofjoin.getFormatedString());
	dialog.find("#email").val(objArray2["email"]);
	dialog.find("#email").attr("readonly",true);
	dialog.find("#pass").val(objArray2["password"]);
	dialog.find("#confirm").val(objArray2["password"]);
	dialog.find("#department").val(objArray2["department"]);
	dialog.find("#userlevel").val(objArray2["userlevel"]);
}

function showUser(id){
	var objArray = new Array();
	if(id!=null&&id!='add'&&id!="next"&&id != "previous"){
		objArray = JSON.parse(id);
	}
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(id == null){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/userService";
	var data = { func: "getAll4Admin",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
					}
	simpleservice(link,data,showUserList);
}

function showUserList(data){
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	var sumtext = "";
	var text = "";
	var pageAmount = objArray2["pageAmount"];
	var line = objArray2["lineAmount"];
	var pagePointer = objArray2["pageNumber"];
	var objArray = objArray2["data"];
	$("#listsize").html("&nbsp;("+objArray2["dataAmount"]+")");
	var lineNumber = (line * pagePointer) - (line-1) ;
	
	if(objArray.length == 0){
		text = "<tr><td colspan=\"7\" style=\"border-top:1px #D6D7D5 solid;border-bottom:1px #D6D7D5 solid;\">"+nodata+"</td></tr>";
		sumtext += text;
	}else{
		for(var i = 0;i< objArray.length+1;i++){
				if(i != objArray.length ){
					var pos = "";
					for(var x=0;x<objArray[i]["pos"].length;x++){
						pos += objArray[i]["pos"][x]+"</br>";
					}
					text = "<tr id=\"row_"+objArray[i]['mappingId']+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(lineNumber+i)+". </td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['mappingId']+"\"> </td>"+
							"<td align=\"center\"> "+objArray[i]['name']+" </td>"+
							"<td align=\"center\"> "+objArray[i]['email']+" </td>"+
							"<td align=\"center\"> "+pos+" </td>"+
							"<td align=\"center\"> "+objArray[i]['userLevel']+" </td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"EditDialog($(this),"+objArray[i]['userId']+")\">Edit</a></td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"changeStatus("+objArray[i]['mappingId']+") \"> " +
							"<span id =status"+objArray[i]['mappingId']+">"+objArray[i]['status']+"</a>" +
							"</td>" +
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showSalutation()\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showSalutation()\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
	}
	$("#showList tr").not($("#showList").find('.tablehead').parent()).remove();
	$("#showList").append(sumtext);
	$("#pageAmount").text(pageAmount);
	if(pagePointer+1 <= pageAmount){
		$("#next").attr("onclick","showSalutation(id)");
		$("#next").html("<a href=\"#\">"+foor_next+"&gt;&gt;</a>");
	}
	if(pagePointer > 1){
		$("#previous").attr("onclick","showSalutation(id)");
		$("#previous").html("<a href=\"#\">&lt;&lt;"+foor_pre+"</a>");
	}
	$("[name=maxEntries]").val(line);
	for ( var i = 0; i < pageAmount; i++) {
		$("[name=selectpage]").append(
				$("<option>").val((i+1)).html((i+1)));
		}
	$("[name=selectpage]").val(pagePointer);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deleteUser(){
	waitingDialog({});
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	if(listCheckBox == ""){
		return false;
	}else{
		var url = "/userService";
		var data = { func : "del",
				id : listCheckBox.substring(0,listCheckBox.length - 1),
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
			}
		simpleservice(url,data,showUserList);
	}
}

function changeStatus(id){
	waitingDialog({});
	var url = "/userService";
	var data = { func : "changeStatus",
			id : id
		}
	simpleservice(url,data,changeStatusSuccess);
}

function changeStatusSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#status"+objArray["id"]).text(objArray["status"]);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}

function fillDialogFrom(dialog){
	yearselectDropdown(dialog.find("#year"));
	monthselectDropdown(dialog.find("#month"));
	generaldayoption(dialog.find("#date"));
	genderDropdown(dialog.find("#gender"));
	bloodDropdown(dialog.find("#blood"));
	print_manageguest_country(dialog.find("#country"));
	var t1 = dialog.find("#country")[0].selectedIndex;
	print_state_bydom(dialog.find("#state"),t1);
	dateofjoin(dialog);
}

function yearselectDropdown(dom)
{
	var date = new Date();
	date.getFullYear();
	for(var hi=1930; hi<=date.getFullYear(); hi++) 
	dom.append("<option value=\""+hi+"\">"+hi+"</option>");
}

function monthselectDropdown(dom)
{
	var month = [Month_Short_Jan,Month_Short_Feb,Month_Short_Mar,
			Month_Short_Apr,Month_Short_May,Month_Short_Jun,
			Month_Short_Jul,Month_Short_Aug,Month_Short_Sep,
			Month_Short_Oct,Month_Short_Nov,Month_Short_Dec
		];
	for(var hi=0; hi<month.length; hi++) 
dom.append("<option value=\""+hi+"\">"+month[hi]+"</option>");
}

function genderDropdown(dom){
	var Array  = JSON.parse('[{"genderValue":"M","genText":"male"},{"genderValue":"F","genText":"female"}]');
	fillDropdown(dom,Array,"genderValue","genText");
}

function bloodDropdown(dom){
	var Array  = JSON.parse('[{"value":"O","text":"O"},{"value":"A","text":"A"},{"value":"B","text":"B"},{"value":"AB","text":"AB"}]');
	fillDropdown(dom,Array,"value","text");
}

function generaldayoption(dom){
	var dayamount = daysinmonth(dom.parent().find("#month").val());
	for(var i = 1;i <= dayamount ;i++){
		dom.append($("<option>").val((i)).html((i)));
	}
}

function checkoptiondate(dom){
	var monthselect = dom.val();
	var dayselect = dom.val(); 
	var daynowsize = dom.size();
	var daynewsize = daysinmonth(monthselect);
	if(daynowsize != daynewsize){
		dom.parent().find("#date option").remove();
		generaldayoption(dom.parent().find("#date"));
	}
}

function daysinmonth(num){
	if(parseInt(num) == 0 || parseInt(num) == 2 || parseInt(num) == 4 || parseInt(num) == 6 ||
			parseInt(num) == 7 || parseInt(num) == 9 || parseInt(num) == 11 ){
		return 31;
	}else if(parseInt(num) == 3 || parseInt(num) == 5 || parseInt(num) == 8 || 
			parseInt(num) == 10){
		return 30;
	}else if(parseInt(num) == 1 ){
		return 28;
	}
}

function dateofjoin(dialog){
	dialog.find("[name=srTxtFrom]").datepicker({
        changeMonth: true,
        changeYear: true,
        showOn: 'button',
        monthNamesShort: ShortMonth,
		monthNames: ShortMonth,
		dayNamesShort: ShortDay,
		dayNames: ShortDay,
        buttonImage: '/images/components/editres/carendar.png',
        buttonImageOnly: true,
        dateFormat: SharedData.General.dateFormat.dateTimeFormat,
        minDate: new Date(),
        onSelect: function(dateText) {
        	dialog.find("[name=srTxtFrom]").attr("date",dateText.getDate().toSystemFormat());
        }
    });
}

function LoadDepartmentAndUserLevel(dialog){
	var url = "/userService";
	var data = { func : "getDepartmentAndUserLevel"
		}
	simpleserviceSyn(url,data,loadSuccess,dialog);
}

function loadSuccess(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var department = objArray["Department"];
	var userlevel = objArray["UserLevel"];
	var dDom = dialog.find("#department");
	var uDom = dialog.find("#userlevel");
	
	for(var i=0; i<department.length; i++) 
		dDom.append("<option value=\""+department[i]["id"]+"\">"+department[i]["title"]+"</option>");
	
	for(var i=0; i<userlevel.length; i++) 
		uDom.append("<option value=\""+userlevel[i]["id"]+"\">"+userlevel[i]["title"]+"</option>");
}